/**
 * represent admin type user
 *
 */

package source;

import java.util.List;
import java.util.Map;

import activejdbc.Model;

public class SystemAdministrator extends Model {
	
	public SystemAdministrator() {
		super();
	}
	
	public SystemAdministrator(Map<String, String> values) {
		this.fromMap(values);
	}
	
	// to be filled
	public void generateReport() {
		
	}
	
	/**
	 *  add user according to input values' type and other fields
	 * 
	 *  values may contain type, username, fullName, age, address ... etc
	 *  SystemAdministrator can add any type ('p', 'n', 'd', 'a') of user
	 * 
	 * @param values	key-map, any values passed in, match them and fill in
	 */
	public void addUser(Map<String, String> values) {
		assert(values.containsKey("type"));
		String type = ((String)values.get("type")).toLowerCase();
		if(type.equals("doctor")) {
			Doctor d = new Doctor(values);
			d.saveIt();
		} else if(type.equals("nurse")) {
			Nurse n = new Nurse(values);
			n.saveIt();
		} else if(type.equals("patient")) {
			Patient p = new Patient(values);
			p.saveIt();
		} else if(type.equals("systemadministrator")) {
			SystemAdministrator s = new SystemAdministrator(values);
			s.saveIt();
		}
	}
	
	
	/**
	 * looks up user by passed in values
	 *  find by id, find by username, find by fullname
	 * 
	 * @param values key-map, any values passed in, match them to look up
	 * @return the user that is found by match
	 */
	public Model lookupUser(Map<String, String> values) {
		assert(values.containsKey("type"));

		String type = ((String)values.get("type")).toLowerCase();
		
		if(type.equals("doctor")) {
			if (values.containsKey("id"))
				return Doctor.findById(values.get("id"));
			else if (values.containsKey("username"))
				return Doctor.findById(values.get("username"));
			else
				return Doctor.findById(values.get("fullname"));
			
		} else if(type.equals("nurse")) {
			if (values.containsKey("id"))
				return Nurse.findById(values.get("id"));
			else if (values.containsKey("username"))
				return Nurse.findById(values.get("username"));
			else
				return Nurse.findById(values.get("fullname"));
			
		} else if(type.equals("patient")) {
			if (values.containsKey("id"))
				return Patient.findById(values.get("id"));
			else if (values.containsKey("username"))
				return Patient.findById(values.get("username"));
			else
				return Patient.findById(values.get("fullname"));
			
		} else if(type.equals("systemadministrator")) {
			if (values.containsKey("id"))
				return SystemAdministrator.findById(values.get("id"));
			else if (values.containsKey("username"))
				return SystemAdministrator.findById(values.get("username"));
			else
				return SystemAdministrator.findById(values.get("fullname"));
			
		} else {
			return null;
		}
	}

	/**
	 * find and update the parameters in user
	 * 
	 * @param values key-map, any values passed in, match them and update
	 * @return user that is updated
	 */
	public void updateUser(Map<String, String> values) {
		assert(values.containsKey("type"));
		String type = ((String)values.get("type")).toLowerCase();
		if(type.equals("doctor")) {
			Doctor d = (Doctor)Doctor.findById(values.get("id"));
			d.fromMap(values);
			d.saveIt();
		} else if(type.equals("nurse")) {
			Nurse n = (Nurse)Nurse.findById(values.get("id"));
			n.fromMap(values);
			n.saveIt();
		} else if(type.equals("patient")) {
			Patient p = (Patient)Patient.findById(values.get("id"));
			p.fromMap(values);
			p.saveIt();
		} else if(type.equals("systemadministrator")) {
			SystemAdministrator s = (SystemAdministrator)SystemAdministrator.findById(values.get("id"));
			s.fromMap(values);
			s.saveIt();
		}
	}
	
	// to be filled
	public void resetPassword(Map<String, String> values) {
		assert(values.containsKey("id"));
		assert(values.containsKey("type"));
		assert(values.containsKey("type"));
		String type = ((String)values.get("type")).toLowerCase();
		String password = values.containsKey("password") ? values.get("password") : "password";
		if(type.equals("doctor")) {
			Doctor d = (Doctor)Doctor.findById(values.get("id"));
			d.set("password", password);
			d.saveIt();
		} else if(type.equals("nurse")) {
			Nurse n = (Nurse)Nurse.findById(values.get("id"));
			n.set("password", password);
			n.saveIt();
		} else if(type.equals("patient")) {
			Patient p = (Patient)Patient.findById(values.get("id"));
			p.set("password", password);
			p.saveIt();
		} else if(type.equals("systemadministrator")) {
			SystemAdministrator s = (SystemAdministrator)SystemAdministrator.findById(values.get("id"));
			s.set("password", password);
			s.saveIt();
		}
	}
	
	// to be filled
	public void suspendUser(Map<String, String> values) {
		assert(values.containsKey("id"));
		assert(values.containsKey("type"));
		String type = ((String)values.get("type")).toLowerCase();
		if(type.equals("doctor")) {
			Doctor d = (Doctor)Doctor.findById(values.get("id"));
			d.set("suspended", 1);
			d.saveIt();
		} else if(type.equals("nurse")) {
			Nurse n = (Nurse)Nurse.findById(values.get("id"));
			n.set("suspended", 1);
			n.saveIt();
		} else if(type.equals("patient")) {
			Patient p = (Patient)Patient.findById(values.get("id"));
			p.set("suspended", 1);
			p.saveIt();
		} else if(type.equals("systemadministrator")) {
			SystemAdministrator s = (SystemAdministrator)SystemAdministrator.findById(values.get("id"));
			s.set("suspended", 1);
			s.saveIt();
		}
	}
	
	// to be filled
	public void unsuspendUser(Map<String, String> values) {
		assert(values.containsKey("id"));
		assert(values.containsKey("type"));
		String type = ((String)values.get("type")).toLowerCase();
		if(type.equals("doctor")) {
			Doctor d = (Doctor)Doctor.findById(values.get("id"));
			d.set("suspended", 0);
			d.saveIt();
		} else if(type.equals("nurse")) {
			Nurse n = (Nurse)Nurse.findById(values.get("id"));
			n.set("suspended", 0);
			n.saveIt();
		} else if(type.equals("patient")) {
			Patient p = (Patient)Patient.findById(values.get("id"));
			p.set("suspended", 0);
			p.saveIt();
		} else if(type.equals("systemadministrator")) {
			SystemAdministrator s = (SystemAdministrator)SystemAdministrator.findById(values.get("id"));
			s.set("suspended", 0);
			s.saveIt();
		}
	}
}
